<?php
/**
 * @author yupoxiong<i@yufuping.com>
 * @title 首页
 */

namespace app\api\controller;
use app\common\model\User;
use app\common\model\ShopConfig;
use app\common\model\UserVip;
use app\common\model\UserVipPay;
use app\common\model\UserCollect;
use app\common\model\Article;
use app\common\model\AccountLog;
use app\common\model\DistributionOrderGoods;
use app\common\validate\UserValidate;
use app\api\logic\UserLogic;
use app\api\logic\WithdrawLogic;
use app\common\logic\AccountLogLogic;
use app\common\model\Feedback;
use app\common\validate\FeedbackValidate;
use app\common\server\UrlServer;
use tools\Crypt;
use think\Request;
use think\Db;
use think\facade\Log;
class UserController extends ApiBase
{

    //用户信息
    public function role_config(Request $request,User $umodel)
    {
        $param = $request->param();
        $user_id =$this->uid;
        if(!$user_id){
            return unauthorized('请先授权');
        }
        $info = UserLogic::center($user_id);
        return success($info);
    }
    


    public function user_edit(Request $request,User $umodel, UserValidate $validate)
    {
        $param = $request->param();
        $user_id =$this->uid;
        if(!$user_id){
            return unauthorized('请先授权');
        }
        //数据验证
        $validate_result = $validate->scene('useredit')->check($param);
        if (!$validate_result) {
             return client_error($validate->getError());
        }
        
        $data['nickname']=$param['nickname']??'';
        $data['mobile']=$param['mobile']??'';
        $data['avatar']=$param['avatar']??'';
        // 启动事务
        $umodel->startTrans();
        try {
            $umodel->where('id',$user_id)->update($data);
            $umodel->commit();
        } catch (Exception $e) {
            $umodel->rollback();
            return client_error($e->getMessage());
        }
        return success('','提交成功');
    }

    //用户流水
    public function AccountLogList(Request $request)
    {
        $param = $request->param();
        $user_id =$this->uid;
        if(!$user_id){
            return unauthorized('请先授权');
        }
        $source = $this->request->get('source');
        $type = $this->request->get('type', 0);
        
        $user_list = UserLogic::accountLog($user_id,$source,$type,$this->page,$this->limit);
        return success($user_list);
    }




    //  //我的粉丝
    // public function fans(Request $request)
    // {
    //     $param = $request->param();
    //     $user_id =$this->uid;
    //     if(!$user_id){
    //         return unauthorized('请先授权');
    //     }
    //     $user_list = UserLogic::fans($user_id,$param,$this->page,$this->limit);
    //     return success($user_list);
    // }


     //提现申请
    public function withdrawapply(Request $request)
    {
        $post = $request->param();
        $user_id =$this->uid;
        if(!$user_id){
            return unauthorized('请先授权');
        }
        $post['user_id'] = $user_id;
        $data=WithdrawLogic::apply($user_id, $post);
        if($data['code']==0){
            return error($data['msg']);
        }   
        return success($data['data'],'提交成功');
    }

    //提现记录
    public function withdrawrecords()
    {
        $get = $this->request->get();
        $user_id =$this->uid;
        if(!$user_id){
            return unauthorized('请先授权');
        }
        $res = WithdrawLogic::records($user_id, $get,$this->page,$this->limit);
        return success($res);
    }

    //意见反馈
    public function feedback(Request $request,Feedback $model,FeedbackValidate $validate)
    {
        $param = $request->param();
        $user_id =$this->uid;
        if(!$user_id){
            return unauthorized('请先授权');
        }
        $validate_result = $validate->scene('add')->check($param);
        if (!$validate_result) {
            return client_error($validate->getError());
        }
        unset($param['uid']);
        $param['user_id']=$user_id;
        $model::create($param);
        return success('','提交成功');
    }

    //用户打卡
    public function user_clockin(Request $request)
    {
        $param = $request->param();
        $user_id =$this->uid;
        if(!$user_id){
            return unauthorized('请先授权');
        }
        // 获取今天日期的0点时间戳
        $todayStart = strtotime(date('Y-m-d 00:00:00'));
        // 获取今天日期的23点59分59秒时间戳
        $todayEnd = strtotime(date('Y-m-d 23:59:59'));

        $user_clockin_count=Db::name('user_clockin')
                ->where(array('user_id'=>$user_id))
                ->where('create_time', '>=', $todayStart)
                ->where('create_time', '<=', $todayEnd)
                ->count();
        if($user_clockin_count>0){
            return error('今日已打卡，请明日在来');
        }
        $integral=ShopConfig::get('website', 'integral');
        $data['user_id']=$user_id;
        $data['integral']=$integral??0;
        $data['create_time']=time();
        // 启动事务
        Db::startTrans();
        try {
            $clockin_id=Db::name('user_clockin')->insertGetId($data);
            if($integral>0){
                $user = User::get($user_id);
                $user->user_integral = ['inc', $integral];
                $source_type = AccountLog::sign_in_integral;
                AccountLogLogic::AccountRecord($user['id'],$integral,1,$source_type,'每日打卡赠送积分');
                $user->save();
            }
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            return client_error('打卡失败');
        }
        return success('','打卡成功');
    }


     //分佣概括
    // public function distribution_order_info(Request $request)
    // {
    //     $param = $request->param();
    //     $user_id =$this->uid??0;
    //     if(!$user_id){
    //         return unauthorized('请先授权');
    //     }
    //     $order_list = UserLogic::distribution_order_info($user_id);
    //     return success($order_list);
    // }

    // //分佣列表
    // public function distribution_order_lists(Request $request)
    // {
    //     $param = $request->param();
    //     $start_data =$param['start_data']??'';
    //     $user_id =$this->uid??0;
    //     if(!$user_id){
    //         return unauthorized('请先授权');
    //     }
    //     $order_list = UserLogic::distribution_order_lists($user_id,$start_data,$this->page,$this->limit);
    //     return success($order_list);
    // }
    
}